package com.junlong0716.enjoylearning.ui

import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.net.api.NetApi
import com.junlong0716.module.common.base.BasePresenter
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import org.json.JSONObject

/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 下午9:22 2018/5/11
 *@modified by:
 */

//06-23 22:37:33.668 4854-4982/com.junlong0716.enjoylearning I/OkGo: 	body:{"code":"200","msg":null,"data":null,"dataMap":{"collectCount":5,"noteQueryCount":3,"learnRecordCount":12,"commentCount":12},"remarks":null}

class KnowledgeListPresenter : BasePresenter<KnowledgeListContract.View>(), KnowledgeListContract.Presenter {

    override fun requestKnowledgeCount(knowledgeListFragment: KnowledgeListFragment) {
        val map = HashMap<String, Int>()
        map["userId"] = MyApp.instance().userAccount!!.id
        val jsonObject = JSONObject(map)
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_USER_KNOWLEDGE_COUNT)
                .tag(knowledgeListFragment)
                .upJson(jsonObject)
                .execute(object :StringCallback(){
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObj = JSONObject(response!!.body())
                        val code = jsonObj.getString("code")
                        if (code == "200"){
                            val collectCount = jsonObj.getJSONObject("dataMap").getInt("collectCount")
                            val noteQueryCount = jsonObj.getJSONObject("dataMap").getInt("noteQueryCount")
                            val learnRecordCount = jsonObj.getJSONObject("dataMap").getInt("learnRecordCount")
                            val commentCount = jsonObj.getJSONObject("dataMap").getInt("commentCount")
                            getView()!!.requestKnowledgeCountSucceed(collectCount,noteQueryCount,learnRecordCount,commentCount)
                        }
                    }
                })
    }

    override fun onDestroy() {

    }
}